package st.shadow.library.core;


import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Interceptor;

/**
 * @author zhangyu
 * @date 2018/9/20
 */

public class Configurator {
    private static final HashMap<Object, Object> SHADOW_CONFIGS = new HashMap<>();
    //对注解IBehavior的拦截处理
   // private static final ArrayList<IBehaviorAction> BEHAVIOR_ACTIONS = new ArrayList<>();
    //字体图片库
    private static final ArrayList<IconFontDescriptor> ICONS = new ArrayList<>();
    //拦截器
    private static final ArrayList<Interceptor> INTERCEPTORS = new ArrayList<>();

    private Configurator() {
        SHADOW_CONFIGS.put(ConfigType.CONFIG_READY, false);
    }

    private static final class Holder {
        private static final Configurator INSTANCE = new Configurator();
    }

    public static Configurator getInstance() {
        return Holder.INSTANCE;
    }

    final HashMap<Object, Object> getShadowConfigs() {
        return SHADOW_CONFIGS;
    }

//    public final Configurator withBehavior(IBehaviorAction behavior) {
//        BEHAVIOR_ACTIONS.add(behavior);
//        SHADOW_CONFIGS.put(ConfigType.METHOD_BEHAVIOR, BEHAVIOR_ACTIONS);
//        return this;
//    }

    public final Configurator withApiHost(String host) {
        SHADOW_CONFIGS.put(ConfigType.API_HOST, host);
        return this;
    }

    public final Configurator withIcon(IconFontDescriptor descriptor) {
        ICONS.add(descriptor);
        SHADOW_CONFIGS.put(ConfigType.ICON, ICONS);
        return this;
    }

    public final Configurator withInterceptor(Interceptor interceptor) {
        INTERCEPTORS.add(interceptor);
        SHADOW_CONFIGS.put(ConfigType.INTERCEPTOR, INTERCEPTORS);
        return this;
    }

    public final void configure() {
        initIcons();
        SHADOW_CONFIGS.put(ConfigType.CONFIG_READY, true);
    }

    @SuppressWarnings("unchecked")
    final <T> T getConfiguration(Object key) {
        final Object value = SHADOW_CONFIGS.get(key);
        if (value == null) {
            return null;
        }
        return (T) SHADOW_CONFIGS.get(key);
    }

    private void initIcons() {
        if (!ICONS.isEmpty()) {
            final Iconify.IconifyInitializer initializer = Iconify.with(ICONS.get(0));
            for (int i = 1, j = ICONS.size(); i < j; i++) {
                initializer.with(ICONS.get(i));
            }
        }
    }
}
